/*
 * @Author: your name
 * @Date: 2022-04-10 19:36:14
 * @LastEditTime: 2022-04-14 20:21:50
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \drivingTow\src\vehicle\vehicle.service.ts
 */
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateVehicleDto, VehicleInfoDto, VehicleRo } from './dto/create-vehicle.dto';
import { UpdateVehicleDto } from './dto/update-vehicle.dto';
import { Vehicle } from './entities/vehicle.entity';

@Injectable()
export class VehicleService {
  constructor(
    @InjectRepository(Vehicle)
    private readonly postsRepository:Repository<Vehicle>
  ){}
  // 创建教练车信息表
  async createVehicle(post):Promise<any>{
    const newPost:Vehicle[] = await this.postsRepository.create({
      ...post
    });
    const created = await this.postsRepository.save(newPost)
    return created
  }

  // 查看所有教练车信息
  async getAllVehicle(post):Promise<VehicleRo>{
    const qb = this.postsRepository
      .createQueryBuilder('Vehicle')
      .where('1=1')
    const Total = await qb.getCount();
    const { pageSize,pageNum } = post
    qb.limit(pageSize)
    qb.offset(pageSize * (pageNum - 1))
    let Vehicle = await qb.getMany();
    const result:VehicleInfoDto[] = Vehicle.map(item => item.toResponseObject())
    return {list:result,Total:Total}
  }

  // 删除此条教练车信息
  async delectVehicle(id):Promise<any>{
    const Vehicle = await this.postsRepository.findOne(id)
    return await this.postsRepository.remove(Vehicle)
  }

  // 修改教练车信息
  async updateVehicle(post):Promise<any>{
    const { licensePlate,Renewal_time,maturity_time,vehicle_status,scrap_date,price} = post;
    console.log();
    const result = await this.postsRepository.update(post.id,{licensePlate:licensePlate,Renewal_time:Renewal_time,
      maturity_time:maturity_time,vehicle_status:vehicle_status,scrap_date:scrap_date,price:price})
    return result
  }
}
